#!/bin/bash
# Word2MD 项目启动脚本 (Unix/Linux/macOS)

set -e  # 遇到错误立即退出

# 项目根目录
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_DIR"

# 虚拟环境路径
VENV_DIR="$PROJECT_DIR/word2md_env"
PYTHON_PATH="$VENV_DIR/bin/python"

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 输出函数
info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

success() {
    echo -e "${GREEN}✅ $1${NC}"
}

warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

error() {
    echo -e "${RED}❌ $1${NC}"
}

# 检查虚拟环境
check_venv() {
    if [ ! -d "$VENV_DIR" ]; then
        warning "虚拟环境不存在，正在创建..."
        python3 -m venv "$VENV_DIR"
        success "虚拟环境创建完成"
    fi

    if [ ! -f "$PYTHON_PATH" ]; then
        error "Python解释器未找到: $PYTHON_PATH"
        exit 1
    fi
}

# 安装依赖
install_deps() {
    info "安装项目依赖..."
    "$PYTHON_PATH" -m pip install --upgrade pip
    "$PYTHON_PATH" -m pip install -r requirements.txt
    success "依赖安装完成"
}

# 启动GUI
run_gui() {
    info "启动Word转Markdown GUI界面..."
    "$PYTHON_PATH" word2md_gui.py
}

# 运行测试
run_test() {
    info "运行转换器测试..."
    "$PYTHON_PATH" test_converter.py
}

# 转换文件
convert_file() {
    local file_path="$1"
    info "转换文件: $file_path"
    "$PYTHON_PATH" word2md.py "$file_path"
}

# 批量转换
batch_convert() {
    local dir_path="$1"
    info "批量转换目录: $dir_path"
    "$PYTHON_PATH" word2md.py "$dir_path" --batch
}

# 格式修复
fix_format() {
    local file_path="$1"
    info "修复格式: $file_path"
    "$PYTHON_PATH" doc_format_fixer.py "$file_path"
}

# 显示帮助
show_help() {
    echo "Word2MD 项目启动脚本"
    echo
    echo "用法: $0 [选项]"
    echo
    echo "选项:"
    echo "  gui                   启动GUI界面"
    echo "  test                  运行测试"
    echo "  setup                 设置开发环境"
    echo "  convert <file>        转换指定文件"
    echo "  batch <dir>           批量转换目录"
    echo "  fix <file>            修复Markdown格式"
    echo "  help                  显示此帮助信息"
    echo
    echo "示例:"
    echo "  $0 gui                      # 启动GUI"
    echo "  $0 convert document.docx    # 转换单个文件"
    echo "  $0 batch ./documents        # 批量转换"
    echo "  $0 fix output.md            # 修复格式"
}

# 设置环境
setup_env() {
    info "设置开发环境..."
    check_venv
    install_deps
    success "环境设置完成！"
    echo
    echo "现在你可以使用以下命令："
    echo "  ./run.sh gui     # 启动GUI界面"
    echo "  ./run.sh test    # 运行测试"
}

# 主函数
main() {
    case "${1:-help}" in
        "gui")
            check_venv
            run_gui
            ;;
        "test")
            check_venv
            run_test
            ;;
        "setup")
            setup_env
            ;;
        "convert")
            if [ -z "$2" ]; then
                error "请指定要转换的文件路径"
                exit 1
            fi
            check_venv
            convert_file "$2"
            ;;
        "batch")
            if [ -z "$2" ]; then
                error "请指定要批量转换的目录路径"
                exit 1
            fi
            check_venv
            batch_convert "$2"
            ;;
        "fix")
            if [ -z "$2" ]; then
                error "请指定要修复格式的Markdown文件路径"
                exit 1
            fi
            check_venv
            fix_format "$2"
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            error "未知选项: $1"
            echo
            show_help
            exit 1
            ;;
    esac
}

# 检查是否为脚本直接执行
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
    main "$@"
fi
